/**
 * SpringMVC restful 风格
 * 共用控制器，控制关于共用的一些业务逻辑操作
 * @author yanwl
 * @date 2014-09-15
 */
package com.chnbst.bstyun.controller.user;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.chnbst.bstyun.core.common.BankpayConfig;
import com.chnbst.bstyun.eos.Eos;
import com.chnbst.bstyun.eos.UserOperator;
import com.chnbst.bstyun.model.pojo.User;
import com.chnbst.bstyun.model.pojo.UserMessage;
import com.chnbst.bstyun.model.pojo.extend.UserRegister;
import com.chnbst.bstyun.service.OrderService;
import com.chnbst.bstyun.service.UserMessageService;
import com.chnbst.bstyun.service.UserService;

@Controller
@RequestMapping("/common")
public class CommonController {
	/**
	 * 注入 业务逻辑层
	 */
	@Autowired
	private UserMessageService userMessageService;
	@Autowired
	private UserService userService;
	@Autowired
	private Eos eos;
	@Autowired
	private BankpayConfig bankpayConfig;
	@Resource 
	private TaskExecutor taskExecutor;//注入Spring封装的异步执行器
	@Autowired
	private OrderService orderService;
	@Autowired
	private UserOperator userOperator;

	/**
	 * post请求 ajax 执行添加用户留言
	 * http://xxx.com/common/usermessage
	 * @return boolean
	 */
	@RequestMapping(value="/usermessage",method=RequestMethod.POST)
	@ResponseBody
	public boolean usermessage(UserMessage userMessage){
		boolean isSuccess = false;
		if(userMessage != null) {
			isSuccess = userMessageService.insertUserMessage(userMessage);
		}
		return isSuccess;
	}
	
	/**
	 * POST请求 检查用户是否登录
	 * @param request
	 * @return boolean
	 */
	@RequestMapping(value = "/checkLogin", method = RequestMethod.POST)
	@ResponseBody
	public boolean checkLogin(HttpServletRequest request) throws Exception {
		boolean isLogin = true;
		User user = (User)request.getSession().getAttribute("userInfo");
		if(user == null) {
			isLogin = false;
		}
		return isLogin;
	}
	
	@RequestMapping(value = "/testP", method = RequestMethod.GET)
	public void testP(HttpServletRequest request) throws Exception {
		/*System.out.println(eos.sendMeal(851));
		//System.out.println(eos.sendProduct(851));
		System.out.println(DESUtil.decrypt("RdhpwAYYyD1rg9LCzy6+7Q=="));*/
		/*Map<String, Object> map = new HashMap<String, Object>();
		map.put("sortField", "pay_time");
		map.put("sortOrder", "desc");
		map.put("userId", 75);
		List<OrderVo> orders = orderService.selectLastOrdersByUserId(map);
		for (OrderVo order : orders) {
			System.out.println(order.getMealName()+"--"+order.getPayTime());
		}*/
//		System.out.println("开始");
//		eos.friend("fanglie5fg.com");
		UserRegister uRegister = new UserRegister();
		uRegister.setLoginName("zs5");
		uRegister.setDisplayName("龙");
		uRegister.setPassword("123456");
		uRegister.setAllowMobileLogin(true);
		uRegister.setMobile("13296749934");
		uRegister.setEnable(true);
		userOperator.register(uRegister);
	}
}
